package com.vertx.web;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.http.HttpServer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;

public class BodyVerticle extends AbstractVerticle {

	@Override
	public void start(Promise<Void> startPromise) throws Exception {
		HttpServer server = vertx.createHttpServer();

		Router router = Router.router(vertx);
		router.route().handler(BodyHandler.create());
		//form-data格式
		router.route("/test/form").handler(req -> {

			String page = req.request().getFormAttribute("page");
			System.out.println("page:" + page);
			req.response()
					.putHeader("content-type", "application/json")
					.end(new JsonObject().put("1", "Hello").put("2", "Vertx").put("3", "!").toString());
		});

		router.route("/test/form2").handler(req -> {

			String body = req.getBodyAsString();
			System.out.println("body:" + body);
			req.response()
					.putHeader("content-type", "application/json")
					.end(new JsonObject().put("1", "Hello").put("2", "Vertx").put("3", "!").toString());
		});
		router.route().handler(BodyHandler.create());
		//form-data格式
		router.route("/test/json").handler(req -> {

			JsonObject j = req.getBodyAsJson();

			System.out.println("jsonObject:" + j.toString());
			req.response()
					.putHeader("content-type", "application/json")
					.end(new JsonObject().put("1", "Hello").put("2", "Vertx").put("3", "!").toString());
		});
		server.requestHandler(router).listen(8080);
	}
}
